<!-- 基础信息 -->
<template>
  <div class="basic_information">
    <Row justify="end">
      <Col>
      <Button class="fieldList_buttonEdit" @click="editField" type="primary"
        v-if="infoData.oaStatus!=5&&infoData.oaStatus!=6&&unlocking('0102010102')">编辑信息</Button>
      <Button class="fieldList_buttonEdit" type="primary" @click="editField('special')"
        v-if="(infoData.oaStatus==5||infoData.oaStatus==6)&&unlocking('0102010103')">特殊调整</Button>
      <Button class="fieldList_buttonEdit" @click="statusLog" type="primary"
        v-if="unlocking('0102010102')">状态变更记录</Button>
      </Col>
    </Row>
    <dt-info ref='info' :labelWidth='120' :options='options'>
    </dt-info>
    <Row :gutter="16" class="product_mess">
      <Col span="3" class="product_label"> 产品信息 </Col>
      <Col span="8" class="product_tips">
      <Icon type="md-alert" class="tips_icon" />
      提示：产品的创建与维护请到
      <span class="product_name" @click="toProduct"> 产品建模 </span>
      页面进行维护
      </Col>
    </Row>
    <Table ref="TableHeight" :max-height="onresizeHeight" :columns="columns" :data="tableData">
    </Table>
    <!-- 编辑弹窗 -->
    <basicInformation-modal ref="infoModal" @onChangeData='init' />
    <!-- 编辑案场状态弹窗 -->
    <basic-status ref="basicStatus" @onChangeData='changeStatus' />
    <!-- 查看OA组织架构 -->
    <check-oa-organiza ref="checkOAOrganiza" />
    <!-- 修改组织架构 -->
    <change-organiza ref="changeOrganiza" @success='onSuccess' />
    <!-- 状态变更记录 -->
    <status-log ref="statusLog" />

    <dt-loading :loading="loading" />
  </div>
</template>
<script>
  import buildingApi from '@/biz/buildingManagement/basicInfo'
  import basicInformationModal from './Modal/basicInformationModal.vue'
  import basicStatus from './Modal/basicStatus.vue'
  import columns from './opt/basicInfoCulomns'
  import checkOaOrganiza from './Modal/checkOAOrganiza.vue'
  import changeOrganiza from './Modal/changeOrganiza.vue'
  import infoOpt from './opt/basicInfoOpt'
  import statusLog from './Modal/statusLog.vue'

  export default {
    components: {
      basicInformationModal,
      basicStatus,
      checkOaOrganiza,
      changeOrganiza,
      statusLog
    },
    data() {
      return {
        from: this.$route.params.type,
        caseLocation: {
          orgName: '', //案场所属业务组织
          orgId: '',
        },
        infoData: {
          status: '', //案场状态
          chairMan: '', //主委
          salesManager: '', //销售经理
          city: '', //所属城市
          enterDate: '', //进场时间
          cooperationMode: '', //合作模式
          agencyRateMin: '', //代理费率低
          agencyRateMax: '', //代理费率高
          developers: '', //开发商
          developerAbbreviation: '', //开发商简称
          address: '', //售楼处地址
          detailedAddress: '', //详细地址
          totalArea: '', //总货量(面积)
          totalPrice: '', //总货值(价值)
          unitArea: '', //单边货量(面积)
          unitPrice: '', //单边货值(价值)
        },
        options: [],
        onresizeHeight: 240,
        tableData: [],
        columns: columns.bankListColumns(this),
        loading: false
      }
    },
    methods: {
      statusLog() {
        this.$refs.statusLog.show()
      },
      //案场组织架构：查询案场对应业务组织架构
      getBuildingOrgName() {
        return buildingApi.getBuildingOrgName({
          buildingId: this.$dt.buildInfo().housesId
        }).then(res => {
          console.log(res)

          return res
        })
      },
      //修改案场所属
      changeOrganiza() {
        let data = {
          firstLine: {
            label: '案场名称：',
            value: this.$dt.buildInfo().housesName,
          },
          secondLine: {
            label: '当前所属：',
            value: this.caseLocation.orgName,
            checkId: this.caseLocation.orgId
          },
          treeTitle: '请选择案场调整后的所属组织：',
        }
        this.$refs.changeOrganiza.show(data)
      },
      onSuccess(val) {
        let params = {
          buildingId: this.$dt.buildInfo().housesId,
          orgId: val
        }
        buildingApi.updBuildingOrg(params).then(res => {
          this.$Message.success('案场所属变更成功!')
          this.init()
        })
      },
      //修改案场状态成功
      changeStatus() {
        this.from = ''
        this.init()
      },
      //查看OA组织架构
      checkOAOrganiza() {
        let data = {
          buildingName: this.$dt.buildInfo().housesName,
          caseLocation: this.infoData.caseLocation + '-' + this.infoData.caseLocationTwo +
            '-' + this.infoData.department,
        }
        this.$refs.checkOAOrganiza.show(data)
      },
      //编辑案场状态
      editStatus() {
        let data = {
          status: this.infoData.status
        }
        this.$refs.basicStatus.show(data)
      },
      //建筑类型映射
      buildingTypeSwitch(productType, buildingTypes) {
        if (!buildingTypes) return '-'
        let newArr = []
        let arr = this.$dt.dictInfo().productType
        for (let i = 0; i < arr.length; i++) {
          if (String(productType) === String(arr[i].key)) {
            arr[i].children.map((item) => {
              buildingTypes.map((itemL) => {
                if (itemL == item.key) {
                  newArr.push(item.value)
                }
              })
            })
          }
        }
        return newArr.join('、')
      },
      initData() {
        let params = {
          housesId: this.$dt.buildInfo().housesId,
        }
        return buildingApi.housesGetDetail(params).then(async (res) => {
          return res
        })
      },
      //编辑信息
      editField(flag) {
        let data = Object.assign({}, this.infoData)
        if (flag == 'special') {
          data.basicInformationFlag = 'special'
          data.statusAttrSort = this.$dt.dictInfo().buildStatus
        } else {
          data.basicInformationFlag = ''
          data.statusAttrSort = this.$dt.dictInfo().buildStatusEdit
        }
        this.$refs.infoModal.show(data)
      },
      //跳转产品建模页面
      toProduct() {
        this.$router.push({
          path: '/buildingModeling/product',
        })
      },
      init() {
        this.loading = true
        Promise.all([this.getBuildingOrgName(), this.initData()]).then(res => {
          console.log(res)
          this.loading = false
          if (res[0]) {
            this.infoData.orgName = res[0].orgName
            this.caseLocation = { ...res[0] }
          }
          if (res[1]) {
            this.tableData = res[1].productDetails ? res[1].productDetails : []
            this.infoData = Object.assign({}, this.infoData, res[1])
            this.options = infoOpt.options(this)
            this.$refs.info.init(this.options, this.infoData)
          }
          if (this.from == 'deal') {
            this.editStatus()
          }
        }).catch(() => {
          this.loading = false
        })
      }
    },
    mounted() {
      this.init()
    },
  }
</script>
<style lang='scss' scoped>
  .basic_information {
    ::v-deep .ivu-btn-text {
      color: #2db7f5;
    }

    .fieldList_buttonEdit {
      margin-left: 10px;
    }

    .product_mess {
      font-size: 16px;
      margin: 20px 0;

      .product_label {
        text-align: center;
      }

      .product_tips {
        font-size: 14px;
        background-color: rgba(254, 252, 236, 1);
        padding-top: 2px;
        border-radius: 8px;

        .product_name {
          color: #2d8cf0;
          cursor: pointer;
        }

        .tips_icon {
          font-size: 16px;
          color: #2d8cf0;
        }
      }
    }
  }
</style>